<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<head>
    <%@ include file="/commons/global.jsp" %>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>花语花坊</title>
    <link rel="stylesheet" href="${pageContext.request.contextPath}/static/css/base.css"/>
    <link rel="stylesheet" href="${pageContext.request.contextPath}/static/css/normalize.css"/>
    <link rel="stylesheet" href="${pageContext.request.contextPath}/static/css/cart.css"/>
    <script src="${pageContext.request.contextPath}/static/js/jquery.min.js"></script>
    <%--<script src="${pageContext.request.contextPath}/static/js/base.js"></script>--%>
    <%--<script src="${pageContext.request.contextPath}/static/js/cart.js"></script>--%>
</head>
<body>
<header>
    <a href="${pageContext.request.contextPath}/order?status=0">
        <img src="${pageContext.request.contextPath}/static/images/header.jpg" height="80"  width="100%" alt="" >
    </a>
</header>

<div class="shortcut">
    <div class="w">
        <ul class="fl city">
            <li><i class="f10"></i><a href="#">天津</a></li>
        </ul>
        <ul class="fr nav">
            <c:if test="${!empty member.member_name}">
                <li>
                    <a href="${pageContext.request.contextPath}/member/getuserdetail" style="display: inline">你好，${member.member_name}</a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                </li>
                <li>
                    <a href="${pageContext.request.contextPath}/member/getloglogin" style="display: inline">退出登录</a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                </li>
            </c:if>


            <%--<li>
                <a href="${pageContext.request.contextPath}/member/getlogin" style="display: inline">登录</a></li>
                <li class="space">
                </li>--%>
            <c:if test="${empty member.member_name}">
                <li class="space"></li>
                <li>
                    <a href="${pageContext.request.contextPath}/getlogin" style="display: inline">你好，请登录</a>
                    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                </li>
                <li>
                    <a href="${pageContext.request.contextPath}/getregister" class="f10"  style="display: inline">免费注册</a>
                </li>
                <li class="space"></li>
                <li>
                    <a href="${pageContext.request.contextPath}/member/getpay"  style="display: inline">我的订单</a>
                </li>
                <li class="space"></li>
            </c:if>

            <li class="space"></li>

            <li>
                <a href="#">客户服务</a>
                <i></i>
                <div class="dropdown-layer">
                    <dl class="d">
                        <dd><a href="#">待处理订单</a></dd>
                        <dd><a href="#">返修退换货</a></dd>
                        <dd><a href="#">降价商品</a></dd>
                    </dl>
                    <dl class="d">
                        <dd><a href="#">消息</a></dd>
                        <dd><a href="#">我的问答</a></dd>
                        <dd><a href="#">我的关注</a></dd>
                    </dl>
                    <dl>
                        <dd><a href="#">我的优惠券</a></dd>
                    </dl>
                </div>
            </li>

            <li class="space"></li>
            <li>
                <div>
                    <a>花语花坊鲜花网：非常棒的鲜花网品牌</a>
                </div>


            </li>

        </ul>
    </div>
</div>
<div class="m">
    <div class="w middle">
        <div class="logo">
            <h1>  <!-- 提高权重 便利于搜索引擎优化 -->
                <a href="${pageContext.request.contextPath}/member/getindex" title="花语花坊">花语花坊</a>
            </h1>
        </div>
        <form class="form" action="${pageContext.request.contextPath}/product/findproductListSelect" method="post">
            <input id="text" name="SelectName" type="text" placeholder="商品搜索">
            <button type="submit"><i></i></button>
        </form>
        <div class="shopCar">
            <i></i><a href="${pageContext.request.contextPath}/member/getcart">购物车</a><span>${countShopping}</span>
        </div>
        <div class="hotwords">
            <a href="#">红玫瑰</a>
            <a href="#">永生花</a>
            <a href="#">生日鲜花</a>
            <a href="#">礼品卡</a>
        </div>
        <div class="navitems">
            <ul>
                <li><a href="${pageContext.request.contextPath}/member/getindex">首页</a></li>
                <li><a href="${pageContext.request.contextPath}/product/getproducandproductimageList">鲜花</a></li>
                <li><a href="${pageContext.request.contextPath}/product/getproducandproductimageList">永生花</a></li>
                <li><a href="${pageContext.request.contextPath}/product/getproducandproductimageList">蛋糕</a></li>
                <li><a href="${pageContext.request.contextPath}/product/getproducandproductimageList">礼品</a></li>
                <li class="space "></li>
                <li><a href="${pageContext.request.contextPath}/product/getproducandproductimageList">巧克力</a></li>
                <li><a href="${pageContext.request.contextPath}/product/getproducandproductimageList">花语大全</a></li>
                <li><a href="${pageContext.request.contextPath}/product/getproducandproductimageList">企业团购</a></li>
                <li><a href="${pageContext.request.contextPath}/product/getproducandproductimageList">设计师甄选鲜花</a></li>
                <li><a href="${pageContext.request.contextPath}/order?status=0">我的订单列表</a></li>
            </ul>
        </div>
    </div>
</div>

<div class="py-container">
    <div class="w">
        <div class="allgoods">
            <h4>全部商品
                <span></span>
            </h4>
            <div class="" style="margin-left: 1000px;margin-top: -25px;width: 500px;margin-bottom: 20px;">
                <input type="button" style="background-color: red;width: 100px;height: 30px;margin-right: 20px" class="searchBtn" value="清空已选中商品" class="removePart" onclick="removePart()">
                <input type="button" style="background-color: red;width: 100px;height: 30px;" class="searchBtn" value="清空购物车" class="removeAll" onclick="removeAll()">
            </div>
            <div class="cart-main">
                <div class="cart-th clearfix" style="width: 1250px">
                    <div><input type="checkbox">全选</div>
                    <div class="">商品</div>
                    <div class="">单价（元）</div>
                    <div class="">数量</div>
                    <div class="">小计（元）</div>
                    <div class="">操作</div>


                </div>
                <div class="cart-title clearfix">
                    <h5 class="fl">传智自营</h5>
                    <div class="yunfei fr">
                        运费：￥6.00
                        <span>还差￥99.00免运费</span>
                    </div>
                </div>
                <div class="cart-item-list">
                    <div class="cart-body">
                        <div class="cart-list">

                            <c:forEach items="${shoppingList}" var="shopping" varStatus="i">
                                <c:if test="${shopping.shopping_checked != 3}">
                                    <ul class="goods-list yui3-g"  style="background-color: #f4636a;margin:5px 0px">
                                        <li class="yui3-u-3-8 pr"data-id="${shopping.product_id}">
                                            <c:if test="${shopping.shopping_checked==0}">
                                                <input type="checkbox" value="${i.count}" id="${i.count}" class="good-checkbox" style="margin-top: 40px">
                                            </c:if>
                                            <c:if test="${shopping.shopping_checked==1}">
                                                <input type="checkbox" value="${shopping.product_id}" class="good-checkbox" checked="checked" style="margin-top: 40px">
                                            </c:if>
                                            <div class="good-item">
                                                <div class="item-img">
                                                    <img src="${shopping.product.productimages_src}" alt="">
                                                </div>
                                                <div class="item-msg">${shopping.product.product_material}</div>
                                            </div>
                                        </li>
                                        <li class="yui3-u-1-8">
                                            <span >${shopping.product.product_name}</span>
                                        </li>
                                        <li class="yui3-u-1-8">
                                            <span class="price">${shopping.product.product_price}</span>
                                        </li>
                                        <li class="yui3-u-1-8">
                                            <div class="clearfix">
                                                <a href="javascript:void(0);" id="${shopping.product_id}" class="increment mins">-</a>
                                                <input autocomplete="off" type="text" id="${shopping.product_id}" value="${shopping.shopping_product_amount}" minnum="1" class="itxt" style="height: 34px">
                                                <a href="javascript:void(0);" id="${shopping.product_id}" class="increment plus">+</a>
                                            </div>
                                            <div class="youhuo">有货</div>
                                        </li>
                                        <li class="yui3-u-1-8">
                                            <span class="sum">${shopping.product.product_price*shopping.shopping_product_amount}</span>
                                        </li>
                                        <li class="yui3-u-1-8">
                                            <div class="del1">
                                                <a href="javascript:;" onclick="delcartproduct(${shopping.product_id})">删除</a>
                                                    <%--<a href="${pageContext.request.contextPath}/cart/delcartproduct?product_id=${shopping.product_id}">删除</a>--%>
                                            </div>
                                                <%--  <div>移到我的关注</div>--%>
                                        </li>
                                    </ul>
                                </c:if>

                            </c:forEach>

                        </div>
                    </div>
                </div>
            </div>
            <div class="cart-tool clearfix">

                <div class="money-box">
                    <div class="sumprice">
                        <div class="sumprice-top">
                                <span>
                                    已选择
                                    <strong>0</strong> 件商品</span>
                            <span>
                                <em>总价（不含运费）：</em>
                                <i class="summoney">￥<span>44240.00</span></i>
                            </span>
                        </div>
                        <div class="sumprice-bottom">
                            已节省：￥20.00
                        </div>
                    </div>
                    <div class="sumbtn">
                        <a class="sum-btn"  href="javascript:void(0)" onclick="zhifu()">结算</a>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<%@include file="/elements/index_bottom_a.html"%>
</body>
<script>
    var pathName=document.location.pathname;
    console.log("pathName="+pathName);
    var index=pathName.substr(1).indexOf("/");
    var result=pathName.substr(0,index+1);
    console.log(result);
    /*${pageContext.request.contextPath}/member/getpay*/
    /**
     * 结算--进入购买流程
     */
    function zhifu() {
        var arr = new Array();
        $(".yui3-u-3-8 input[type='checkbox']").each(function () {
            if (this.checked){
                /*if ($(".yui3-u-3-8 input[type='checkbox']").attr("checked")==true)*/
                var $id = $(this).parent().attr("data-id");
                var obj = new Object();
                obj.id = $id;
                arr.push(obj);
                product_id = $(this).parent().attr("data-id");
                console.log("product_id="+product_id);
                $.ajax({
                    type:"get",
                    url:result+"/cart/updateCartChecked",
                    data:{product_id:product_id,shopping_checked:1},
                    dataType:"json",
                    success:function (data) {
                        if (data.success){
                            console.log("good-checkbox标记成功");
                        }else {
                            console.log("标记失败");
                        }
                    }
                });
            }else {
                var $id = $(this).parent().attr("data-id");
                var obj = new Object();
                obj.id = $id;
                arr.push(obj);
                product_id = $(this).parent().attr("data-id");
                console.log("product_id="+product_id);
                $.ajax({
                    type:"get",
                    url:result+"/cart/updateCartChecked",
                    data:{product_id:product_id,shopping_checked:0},
                    dataType:"json",
                    success:function (data) {
                        if (data.success){
                            console.log("good-checkbox取消标记成功");
                        }else {
                            console.log("取消标记失败");
                        }
                    }
                });
            }

        });

        /*if(arr.length==0){
            alert("请至少选择一个商品结算");
            return false;
        }*/
        console.log("aaa");
        $.ajax({
            type:"get",
            url:result+"/order/paycar",
            data:{shopping_checked:1},
            dataType:"json",
            success:function (data) {
                if (data.success){
                    window.location.href="${pageContext.request.contextPath}/member/getpay";
                    console.log("商品数量增加成功");
                }else {

                }
            }
        });
    }

</script>
<script>
    var pathName=document.location.pathname;
    console.log("pathName="+pathName);
    var index=pathName.substr(1).indexOf("/");
    var result=pathName.substr(0,index+1);
    console.log(result);

    function delcartproduct(id) {
        /*int member_id,int product_id,int shopping_product_amount*/
        $.ajax({
            url:result+"/cart/delcartproduct",
            type:"get",
            data:{"product_id":id},
            dataType:"json",
            success:function (data) {
                if (data.success){
                    //alert("删除商品成功");
                    location.reload();
                }else {
                    alert("您好，请添加商品到购物车！");
                }
            }
        })
    }
    function removeAll() {
        var r=confirm("是否要清空购物车所有商品？？")
        if (r=true){
            /*int member_id,int product_id,int shopping_product_amount*/
            $.ajax({
                url:result+"/cart/delCartProductById",
                type:"get",
                data:{},
                dataType:"json",
                success:function (data) {
                    if (data.success){
                        location.reload();
                        alert("清空购物车成功");
                    }else {
                        alert("您好，请添加商品到购物车！");
                    }
                }
            })
        }

    }
    function removePart() {
        var arr = new Array();
        var r=confirm("是否要清空已选中商品？？")
        if (r==true){
            var arr = new Array();
            $(".yui3-u-3-8 input[type='checkbox']").each(function () {
                if (this.checked){
                    /*if ($(".yui3-u-3-8 input[type='checkbox']").attr("checked")==true)*/
                    var $id = $(this).parent().attr("data-id");
                    var obj = new Object();
                    obj.id = $id;
                    arr.push(obj);
                    product_id = $(this).parent().attr("data-id");
                    console.log("product_id="+product_id);
                    $.ajax({
                        type:"get",
                        url:result+"/cart/updateCartChecked",
                        data:{product_id:product_id,shopping_checked:1},
                        dataType:"json",
                        success:function (data) {
                            if (data.success){
                                console.log("good-checkbox标记成功");
                            }else {
                                console.log("标记失败");
                            }
                        }
                    });
                }else {
                    var $id = $(this).parent().attr("data-id");
                    var obj = new Object();
                    obj.id = $id;
                    arr.push(obj);
                    product_id = $(this).parent().attr("data-id");
                    console.log("product_id="+product_id);
                    $.ajax({
                        type:"get",
                        url:result+"/cart/updateCartChecked",
                        data:{product_id:product_id,shopping_checked:0},
                        dataType:"json",
                        success:function (data) {
                            if (data.success){
                                console.log("good-checkbox取消标记成功");
                            }else {
                                console.log("取消标记失败");
                            }
                        }
                    });
                }

            });
            console.log("清除部分商品成功");
            /*if(arr.length==0) {
                alert("没有选择一个商品， 删除失败");
                return false;
            }*/
            $.ajax({
                type:"get",
                url:result+"/cart/deleteCartChecked",
                data:{},
                dataType:"json",
                success:function (data) {
                    if (data.success){
                        console.log("清除部分商品成功");
                        location.reload();
                    }else {
                        console.log("标记失败");
                    }
                }
            });

        }

    }
/*
    function removePart() {
        var arr = new Array();
        var r=confirm("是否要清空已选中商品？？")
        if (r=true){
            $(".yui3-u-3-8 input[type='checkbox']").each(function () {
                if (this.checked){
                    /!*if ($(".yui3-u-3-8 input[type='checkbox']").attr("checked")==true)*!/
                    var $id = $(this).parent().attr("data-id");
                    var obj = new Object();
                    obj.id = $id;
                    arr.push(obj);
                    product_id = $(this).parent().attr("data-id");
                    console.log("product_id="+product_id);
                    $.ajax({
                        type:"get",
                        url:result+"/cart/updateCartChecked",
                        data:{product_id:product_id,shopping_checked:1},
                        dataType:"json",
                        success:function (data) {
                            if (data.success){
                                console.log("good-checkbox标记成功");
                            }else {
                                console.log("标记失败");
                            }
                        }
                    });
                }else {
                    var $id = $(this).parent().attr("data-id");
                    var obj = new Object();
                    obj.id = $id;
                    arr.push(obj);
                    product_id = $(this).parent().attr("data-id");
                    console.log("product_id="+product_id);
                    $.ajax({
                        type:"get",
                        url:result+"/cart/updateCartChecked",
                        data:{product_id:product_id,shopping_checked:0},
                        dataType:"json",
                        success:function (data) {
                            if (data.success){
                                console.log("good-checkbox取消标记成功");
                            }else {
                                console.log("取消标记失败");
                            }
                        }
                    });
                }

            });
            console.log("清除部分商品成功");
            /!*if(arr.length==0) {
                alert("没有选择一个商品， 删除失败");
                return false;
            }*!/
            $.ajax({
                type:"get",
                url:result+"/cart/deleteCartChecked",
                data:{},
                dataType:"json",
                success:function (data) {
                    if (data.success){
                        console.log("清除部分商品成功");
                        location.reload();
                    }else {
                        console.log("标记失败");
                    }
                }
            });

        }

    }
*/
/*
    function removePart() {
        var arr = new Array();
        var r=confirm("是否要清空已选中商品？？")
        if (r=true){
            $(".yui3-u-3-8 input[type='checkbox']").each(function () {
                if (this.checked){
                    var $id = $(this).parent().attr("data-id");
                    var obj = new Object();
                    obj.id = $id;
                    arr.push(obj);
                    product_id = $(this).parent().attr("data-id");
                    console.log("product_id="+product_id);
                    $.ajax({
                        type:"get",
                        url:result+"/cart/deleteCartChecked",
                        data:{product_id:product_id,shopping_checked:1},
                        dataType:"json",
                        success:function (data) {
                            if (data.success){
                                console.log("清除部分商品成功");
                            }else {
                                console.log("标记失败");
                            }
                        }
                    });
                }



            });
        }
        location.reload();
        if(arr.length==0) {
            alert("没有选择一个商品， 删除失败");
            return false;
        }

    }
*/


</script>
<script>
    $(function() {
        var pathName=document.location.pathname;
        console.log("pathName="+pathName);
        var index=pathName.substr(1).indexOf("/");
        var result=pathName.substr(0,index+1);
        console.log(result);
        $(".cart-list ul").mouseover(function() {
            $(this).addClass("active").siblings().removeClass("active");
        });
        $(".cart-th input[type='checkbox']").click(function() {
            if (this.checked) {
                $(".yui3-u-3-8 input[type='checkbox']").prop("checked",true);
                $.ajax({
                    type:"get",
                    url:result+"/cart/updateCartCheckedAll",
                    data:{shopping_checked:1},
                    dataType:"json",
                    success:function (data) {
                        if (data.success){
                            console.log("标记成功");
                        }else {
                            console.log("标记失败");
                        }
                    }
                });
            }
            else {
                $(".yui3-u-3-8 input[type='checkbox']").prop("checked",false);
                $.ajax({
                    type:"get",
                    url:result+"/cart/updateCartCheckedAll",
                    data:{shopping_checked:0},
                    dataType:"json",
                    success:function (data) {
                        if (data.success){
                            console.log("取消标记成功");
                        }else {
                            console.log("标记失败");
                        }
                    }
                });
            }
            me_sum();
        });

        $(".good-checkbox").click(function() {
            me_sum();
            var flag = true;
            for (var i = 0; i < $(".good-checkbox").length; i++) {
                if (! $(".good-checkbox")[i].checked) {
                    flag = false;
                    break;
                }
            }
            $(".cart-th input[type='checkbox']").prop("checked",flag);
        });
        $(document).ready(function(){
            me_sum();
        });
        function me_sum() {
            var sum = 0;
            for (var i = 0; i < $(".goods-list").length; i++) {
                if ($(".goods-list").eq(i).children(".yui3-u-1-8").eq(3).children().html() == undefined) {
                    console.log($(".goods-list").eq(i).children(".yui3-u-1-8").eq(3).children().html());
                    continue;
                }
                else if (! $(".good-checkbox")[i].checked) {
                    continue;
                }
                else {
                    sum = sum + Number($(".goods-list").eq(i).children(".yui3-u-1-8").eq(3).children().html());
                }
            }
            $(".summoney span").html(sum.toFixed(2));
        };
        $(".goods-list .plus").click(function() {
            var num = $(this).siblings(".itxt").val();

            //当前商品的购物车实时数量
            var m = $(this).siblings(".itxt").val(Number(num)+1).val();

            //当前商品的产品id
            var id=$(this).attr('id');
            //alert(id);
            console.log("sum_plus_m="+m);

            //向前端发送请求，进行数据库的数量实时更新购物车的数量
            /!*int member_id,int product_id,int shopping_product_amount*!/
            $.ajax({
                url:result+"/cart/updateCartProduct",
                type:"get",
                data:{"product_id":id,"shopping_product_amount":m},
                dataType:"json",
                success:function (data) {
                    if (data.success){
                        //alert("商品数量更新成功");
                        //location.reload();
                        console.log("商品数量增加成功");
                    }else {
                        alert("您好，请添加商品到购物车！");
                    }
                }
            })

            var sum = ($(this).parent().parent().siblings(".yui3-u-1-8").eq(1).children(".price").html() * m).toFixed(2);
            console.log("sum_plus="+sum);


            $(this).parent().parent().siblings(".yui3-u-1-8").eq(2).children(".sum").html(sum);
            me_sum();
        });
        $(".goods-list .mins").mouseover(function() {
            if ($(this).siblings(".itxt").val() <= 1) {
                $(this).css("cursor","not-allowed");
            }
            else {
                $(this).css("cursor","pointer");
            }
        });
        $(".goods-list .mins").click(function() {
            if ($(this).siblings(".itxt").val() <= 1) {
                $(this).css("cursor","not-allowed");
            }
            else {
                $(this).css("cursor","pointer");
                num =  $(this).siblings(".itxt").val();
                $(this).siblings(".itxt").val(num-1);

                //当前商品的购物车实时数量
                var count=num-1;
                console.log("sum_mins_count="+count);
                //当前商品的产品id
                var id=$(this).attr('id');
                //alert(id);
                //向前端发送请求，进行数据库的数量实时更新购物车的数量
                /!*int member_id,int product_id,int shopping_product_amount*!/
                $.ajax({
                    url:result+"/cart/updateCartProduct",
                    type:"get",
                    data:{"product_id":id,"shopping_product_amount":count},
                    dataType:"json",
                    success:function (data) {
                        if (data.success){
                            /*alert("商品数量更新成功");*/
                            console.log("商品数量减少成功");
                            /*location.reload();*/
                        }else {
                            alert("您好，请添加商品到购物车！");
                        }
                    }
                })

                var m = $(this).parent().parent().siblings(".yui3-u-1-8").eq(1).children(".price").html();
                console.log("sum_mins_m="+m);

                var sum = $(this).parent().parent().siblings(".yui3-u-1-8").eq(2).children(".sum").html();

                console.log("sum_mins="+sum);
                $(this).parent().parent().siblings(".yui3-u-1-8").eq(2).children(".sum").html((sum - m).toFixed(2));
                me_sum();
            }
        });
        $(".del1 a").click(function() {
            $(this).parent().parent().siblings().remove();
            $(this).parent().parent(".cart-list ul").remove();
            $(this).closest('.goods-list').remove();
            me_sum();
        });
        $(".removeAll").click(function() {

            var sum=0;
            $(".summoney span").html(sum.toFixed(2));
        });
    });

    $(function() {
        $(".nav>li").mouseover(function() {
            $(this).children(".dropdown-layer").show();
        });
        $(".nav>li").mouseout(function() {
            $(this).children(".dropdown-layer").hide();
        });
        $(".close-btn").click(function() {
            $(this).hide().siblings("img").hide();
        });
    });
</script>

